package com.vm.osm.business.controller;


import com.vm.osm.business.common.Constants;
import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.ErrCode;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.util.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/3/28.
 */

@RestController
@RequestMapping("/v1.0")
@Api(value = "fileController",description = "文件上传",tags = {"FileController"})
public class FileController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());


    private static final String FILE_SEPARATOR = "/";
    /**
     * upload 图片上传方法
     * 
     * @throws IOException
     */
    @ApiOperation(value = "文件上传",notes = "文件上传")
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(@RequestParam("editormd-image-file") MultipartFile multipartFile) throws IOException {
        //根据时间生成文件夹目录
        if (multipartFile.isEmpty() || StringUtils.isEmpty(multipartFile.getOriginalFilename())) {
            throw new BusinessException(ErrCode.PARAM_NOT_RIGHT.toString());
        }
        String contentType = multipartFile.getContentType();
        if (!contentType.contains("")) {
            throw new BusinessException(ErrCode.IMG_FORMAT_ERROR.toString());
        }
        String rootFileName = multipartFile.getOriginalFilename();
        LOG.info("上传图片:name={"+rootFileName+"},type={"+contentType+"}" );

        String upToken = QiniuUtils.getQiNiuToken(Constants.bucket);

//        String path = "/Users/chenzui/my-study/";
        String path = "/alidata01/chenz/temp-img/";
        File dest = new File(path + "/" + rootFileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        multipartFile.transferTo(dest);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除

        String res = QiniuUtils.upFileToQiNiuZone(upToken, dest.getAbsolutePath());
        if (dest.exists() && dest.isFile()) {
            if (dest.delete()) {
                System.out.println("删除单个文件" + rootFileName + "成功！");
            } else {
                System.out.println("删除单个文件" + rootFileName + "失败！");
            }
        } else {
            System.out.println("删除单个文件失败：" + rootFileName + "不存在！");
        }
        return Constants.QINIU_URL+FILE_SEPARATOR+res;

    }


    /**
     * 根据时间生成文件夹目录
     * @return
     */




//    public static void main(String[] args) throws FileNotFoundException {
//        Date nowTime = new Date(System.currentTimeMillis());
//        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//        String retStrFormatNowDate = sdFormatter.format(nowTime);
//        retStrFormatNowDate += "." + "jpg";
////        String newFilePath = GetPathByDate.getPath();
//        InputStream in = new FileInputStream(new File("/Users/chenzui/Documents/成都天府新区.png"));
////        FtpUtil.uploadFile(ftpUrl, 21, ftpUsername, ftpPassword, retStrFormatNowDate, in,newFilePath);
//
//    }


}
